//
//  MYMainViewController.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/19.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit

class MYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //self.view.backgroundColor = UIColor.redColor()
        
        addChildViewControllers()
        
        let mainTabBar = MYTabBar()
        
        mainTabBar.plusBtn.addTarget(self, action: #selector(MYMainViewController.plusBtnClick), forControlEvents: .TouchUpInside)
        
        
        setValue(mainTabBar, forKey: "tabBar")
        
        
    }
    
    
    @objc private func plusBtnClick() {
        //print("加号按钮的点击方法")
        
        //把mainVC当参数传递到composeView
        let composeView = MYComposeView()
        composeView.show(targetVC: self)
    }
    
    
    private func addChildViewControllers() {
        
        addChildViewController(MYHomeViewController(), title: "首页", imageName: "tabbar_home", index: 0)
        addChildViewController(MYMessageViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(MYDiscoverViewController(), title: "发现", imageName: "tabbar_discover", index: 2)
        addChildViewController(MYProfileViewController(), title: "我的", imageName: "tabbar_profile", index: 3)

        
    }
    
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String, index: Int) {
        
        //vc.view.backgroundColor = UIColor.cyanColor()
        //如果导航条&&tabBar的title一样的话可以这样设置
        vc.title = title;
        //vc.tabBarItem.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = index
        //后面部分设置渲染的模式
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置选中时文字颜色
       vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        //提高tabBarItem的高度(提高文字高度)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        let nav = MYBasicNavViewController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    
    
    
    //设置tabBarButoon的点击动画
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        var index = 0
        for v in tabBar.subviews {
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if item.tag == index {
                    for subview in v.subviews {
                        if subview.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            //制作动画
                            subview.transform = CGAffineTransformMakeScale(0.6, 0.6)
                            UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
                                subview.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                            })
                        }
                    }
                }
                index += 1
            }
        }
     }
    
        
    


}
